package cn.wolfcode.wolf2w.web.controller;

import cn.wolfcode.wolf2w.annotation.RequestLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.cache.service.IStrategyRedisService;
import cn.wolfcode.wolf2w.cache.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.domain.UserInfo;

import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("users")
public class UserInfoController {
    // 短信发送的接口路径
    @Value("${sms.url}")
    private String smsUrl;
    // 短信发送的凭证
    @Value("${sms.appkey}")
    private String smsKey;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private IStrategyRedisService strategyRedisService;

    /**
     * 模拟页面直接获取数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public Object get(String id){
        UserInfo userInfo = userInfoService.get(id);

        return userInfo;
    }

    /**
     * 判断手机号码是否注册
     * @param phone
     * @return
     */
    @GetMapping("checkPhone")
    public Object checkPhone(String phone){
        boolean ret = userInfoService.checkPhone(phone);
        // 对返回来的结果取反
        return !ret;
    }

    /**
     *发送短信验证
     * @param phone
     * @return
     */
    @GetMapping("sendVerifyCode")
    public Object sendVerifyCode(String phone){
        //使用业务层方法发送短信 , 参数为页面传的手机号
        //smsUrl:短信发送的接口路径   // smsKey: 短信发送的凭证
        userInfoService.sendVerifyCode(phone,smsUrl,smsKey);

        return JsonResult.success();
    }

    /**
     *完成用户注册
     * @param nickname
     * @param password
     * @param rpassword
     * @param verifyCode
     * @param phone
     * @return
     */
    @PostMapping("regist")
    public Object regist(String nickname,String password,String rpassword,String verifyCode,String phone){

        userInfoService.regist(nickname,password,rpassword,verifyCode,phone);

        return JsonResult.success();
    }

    @PostMapping("login")
    public Object login(String username,String password){
        UserInfo userInfo = userInfoService.login(username, password);

        String token = userInfoRedisService.setRedis(userInfo);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",userInfo);

        /**
         * {
         *     code:
         *     msg:
         *     data:{
         *         token 用于下次请求是将token信息带过来进行对比
         *         user  登陆成功,页面想回显页面信息
         *     }
         * }
         */

        return JsonResult.success(map); // 返回data数据
    }

    /**
     * 浏览器第二次发送请求时,重置存活时间
     * @param request
     * @return
     */
    @RequestLogin
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request){
        // 通过请求对象获取token
     String token = request.getHeader("token");
        System.out.println("从浏览器中拿到的token: " + token);
        // 通过token查询到redis中key对应的value
       UserInfo userInfo = userInfoRedisService.getUserByToken(token);
      //
       return JsonResult.success(userInfo);
    }

    /**
     * 获取用户收藏的攻略集id
     * @param uid
     * @return
     */
    @GetMapping("strategies/favor")
    public Object getUserSidList(String uid, @UserParam UserInfo userInfo){
        List<String> sidList = strategyRedisService.getUserSidList(userInfo.getId());
        return JsonResult.success(sidList);
    }

}
